package com.ds.infrastructure.audit.collector.config;

import com.ds.infrastructure.audit.collector.constant.CollectorConstant;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/7/31
 */
//@Component
public class ElasticsearchConfig {

    /**
     * elk集群地址
     */
    @Value("${elasticsearch.ip}")
    private String hostName;
    /**
     * 端口
     */
    @Value("${elasticsearch.port}")
    private String port;
    /**
     * 集群名称
     */
    @Value("${elasticsearch.clusterName}")
    private String clusterName;

    /**
     * 连接池
     */
    @Value("${elasticsearch-new.pool}")
    private String poolSize;

    private ElasticsearchConfig() {
    }

    @Bean(value = "lq-es")
    public RestHighLevelClient initClient() {
        String[] ips = hostName.split(CollectorConstant.SPLIT_SIGN);
        String[] ports = port.split(CollectorConstant.SPLIT_SIGN);
        List<HttpHost> httpHostList = new ArrayList<>();
        for (int i = 0; i < ips.length; i++) {
            HttpHost httpHost = new HttpHost(String.valueOf(ips[i]), Integer.valueOf(ports[i]), "http");
            httpHostList.add(httpHost);
        }
        return new RestHighLevelClient(RestClient.builder(httpHostList.toArray(new HttpHost[httpHostList.size()])));
    }

}

